#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.

# This file written by Dhritiman Sagar.

from pydht import dht, dhtutil

from mojito_analysis import scan_parser

import event
import logging
import random
import signal
import sys
import gzip
import glob
import time

class DHTClient(dht.DHTClient):
  def __init__(self, addr, dht_id=None, firewalled=False):
    dht.DHTClient.__init__(self, addr)
    self.node_count = 0
    self.node_set = []
    self.count_pending = 0
#    self.outfyl = open("stats." + time.strftime("%Y-%m-%d.%H:%M-%S", time.localtime(time.time())) + ".out", "w")
    
  def handle_ping_reply(self, reply, sender, *args):
    print 'Reply received from %s' % str(sender)
    print reply

  def handle_find_node_reply(self, reply, sender, *args):
    print 'Reply received from %s' % str(sender)
    print reply
    
  def handle_stats_reply(self, reply, sender, *args):
  #  outfyl.writeline(reply.router_uptime)
    print (reply.router_uptime, reply.total_bytes_received, reply.total_bytes_sent,
           reply.total_packets_received, reply.total_packets_sent,
           reply.db_key_count, reply.db_value_count, reply.db_store_size, reply.router_contacts,
           reply.router_nodes, reply.router_leaves)

  def send_messages(self):
    for ip, port in self.node_set:
      client.send_stats_request(dht.make_contact((ip, port)))

  def main(self):
    dhtutil.the_send_queue.send_slowed_callback = event.abort
    dhtutil.HACK_DISABLE_TIMER = False
    files = glob.glob("scan.Vuze.*.gz")
    for file in files:
      self.node_set.extend(scan_parser.slurp_responsive_nodes(file))
    event.timeout(1, self.send_messages)
      
      
if __name__ == '__main__':
  logging.basicConfig(filename="vp.log", level=logging.DEBUG)
  ext_ip = sys.argv[1]
  client = DHTClient((ext_ip, 7900))
  event.signal(signal.SIGTERM, event.abort)
  event.signal(signal.SIGINT, event.abort)
  event.timeout(1, client.main)
  event.dispatch()
